package com.tianxin.workbao.workBao.utils.common;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.tianxin.workbao.workBao.bean.CarFinance;
import com.tianxin.workbao.workBao.bean.CarInformation;
import com.tianxin.workbao.workBao.bean.Coal;
import com.tianxin.workbao.workBao.bean.Fueldiary;
import com.tianxin.workbao.workBao.bean.LineUp;
import com.tianxin.workbao.workBao.bean.Loader;
import com.tianxin.workbao.workBao.bean.LoaderUse;
import com.tianxin.workbao.workBao.bean.MobileChart;
import com.tianxin.workbao.workBao.bean.OilPrice;
import com.tianxin.workbao.workBao.bean.Organization;
import com.tianxin.workbao.workBao.bean.Sampling;
import com.tianxin.workbao.workBao.bean.Storage;
import com.tianxin.workbao.workBao.bean.TestItem;
import com.tianxin.workbao.workBao.bean.User;
import com.tianxin.workbao.workBao.common.Constant;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by hacker on 2017/6/20.
 */

public class BeansUtil {

    public static String getNumber(String header) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String format = sdf.format(new Date());
        return (header + "-" + format);
    }

    public static Organization organizationToBean(String json) throws Exception {
        Organization organization = new Organization();
        JSONObject temp = new JSONObject(json);
        organization = new Organization();
        return organizationTo(organization, temp);
    }

    public static List<Organization> organizationToBeans(String json) throws Exception {
        List<Organization> organizations = new ArrayList<>();
        Organization organization = null;
        JSONArray array = new JSONArray(json);
        for (int i = 0; i < array.length(); i++) {
            organization = new Organization();
            JSONObject temp = (JSONObject) array.get(i);
            organizations.add(organizationTo(organization, temp));
        }
        return organizations;
    }

    private static Organization organizationTo(Organization organization, JSONObject object) throws Exception {
        organization.setId(Long.valueOf(object.getInt("id")));
        organization.setAddtime(TimeUtils.getDateTimeFromStamp(object.getLong("addtime")));
        organization.setIsis(object.getInt("isis"));
        organization.setOgname(object.getString("ogname"));
        organization.setPid(Long.valueOf(object.getInt("pid")));
        organization.setStatus(object.getInt("status"));
        return organization;
    }

    public static User userToBean(String json) throws Exception {
        User user = new User();
        JSONObject temp = new JSONObject(json);
        return userTo(user, temp);
    }

    public static List<User> userToBeans(String json) throws Exception {
        List<User> users = new ArrayList<>();
        JSONArray array = new JSONArray(json);
        User user;
        for (int i = 0; i < array.length(); i++) {
            user = new User();
            JSONObject temp = (JSONObject) array.get(i);
            users.add(userTo(user, temp));
        }
        return users;
    }

    private static User userTo(User user, JSONObject object) throws Exception {
        user.setApploginnum(object.getInt("apploginnum"));
        try {
            user.setApptoken(object.getString("apptoken"));
            user.setApplogintime(TimeUtils.getDateTimeFromStamp(object.getLong("applogintime")));
            user.setLastlogintime(TimeUtils.getDateTimeFromStamp(object.getLong("lastlogintime")));
            user.setBirthday(TimeUtils.getDateTimeFromStamp(object.getLong("birthday")));
            user.setCreatetime(TimeUtils.getDateTimeFromStamp(object.getLong("createtime")));
            user.setSalt(object.getString("salt"));
        } catch (Exception e) {
        }
        user.setDuty(object.getString("duty"));
        user.setHeadphoto(object.getString("headphoto"));
        user.setEmail(object.getString("email"));
        user.setId(Long.valueOf(object.getInt("id")));
        user.setIsis((byte) object.getInt("isis"));
        user.setLevel(object.getInt("level"));
        user.setLoginnum(object.getInt("loginnum"));
        user.setMobiletel(object.getString("mobiletel"));
        user.setName(object.getString("name"));
        user.setOfficetel(object.getString("officetel"));
        user.setOrgid(object.getInt("orgid"));
        user.setOrgname(object.getString("orgname"));
        user.setPassword(object.getString("password"));
        user.setPorgid(object.getInt("porgid"));
        user.setPorgname(object.getString("porgname"));
        user.setQq(object.getString("qq"));
        user.setSex(object.getString("sex"));
        user.setStatus((byte) object.getInt("status"));
        user.setUsername(object.getString("username"));
        user.setWeixin(object.getString("weixin"));
        return user;
    }

    public static LineUp lineUpToBean(String json) throws Exception {
        LineUp lineUp = new LineUp();
        JSONObject temp = new JSONObject(json);
        return lineUpTo(lineUp, temp);
    }

    public static List<LineUp> lineUpToBeans(String json) throws Exception {
        List<LineUp> list = new ArrayList<>();
        JSONArray array = new JSONArray(json);
        LineUp lineUp;
        for (int i = 0; i < array.length(); i++) {
            lineUp = new LineUp();
            JSONObject temp = (JSONObject) array.get(i);
            list.add(i, lineUpTo(lineUp, temp));
        }
        return list;
    }

    private static LineUp lineUpTo(LineUp lineUp, JSONObject object) throws Exception {

        lineUp.setId(Long.valueOf(object.getInt("id")));
        lineUp.setNumber(object.getString("number"));
        lineUp.setAddtime(TimeUtils.getDateTimeFromStamp(object.getLong("addtime")));
        lineUp.setBillnumber(object.getString("billnumber"));
        lineUp.setBuycompany(object.getString("buycompany"));
        lineUp.setBuycompanyid(object.getInt("buycompanyid"));
        lineUp.setCarconnectphone(object.getString("carconnectphone"));
        lineUp.setCarnumber(object.getString("carnumber"));
        lineUp.setCarnumberid(object.getInt("carnumberid"));
        lineUp.setCarowner(object.getString("carowner"));
        lineUp.setCarowneridnum(object.getString("carowneridnum"));
        lineUp.setCoaltype(object.getString("coaltype"));
        lineUp.setCoaltypeid(object.getInt("coaltypeid"));
        lineUp.setContractid(object.getInt("contractid"));
        lineUp.setContractnum(object.getString("contractnum"));
        lineUp.setIsloadcar(object.getInt("isloadcar"));
        lineUp.setIsup(object.getInt("isup"));
        lineUp.setLineupuserid(object.getInt("lineupuserid"));
        lineUp.setLineupusername(object.getString("lineupusername"));
        lineUp.setSalecompany(object.getString("salecompany"));
        lineUp.setSalecompanyid(object.getInt("salecompanyid"));
        lineUp.setStatus(object.getInt("status"));
        lineUp.setType(object.getInt("type"));
        try {
            lineUp.setLastupdatetime(TimeUtils.getDateTimeFromStamp(object.getLong("lastupdatetime")));
            lineUp.setUpdateuserid(object.getInt("updateuserid"));
            lineUp.setUpdateusername(object.getString("updateusername"));
        } catch (Exception e) {
            Log.d("----------->", "lineUpTo: " + e.getMessage());
        }
        try {
            lineUp.setLoadcarid(object.getInt("loadcarid"));
            lineUp.setLoaduserid(object.getInt("loaduserid"));
            lineUp.setLoadtime(TimeUtils.getDateTimeFromStamp(object.getLong("loadtime")));
            lineUp.setLoadusername(object.getString("loadusername"));
            lineUp.setMoney(Float.valueOf(object.getString("money")));
            lineUp.setRemark(object.getString("remark"));
            lineUp.setTags(object.getString("tags"));
            lineUp.setLoadcarname(object.getString("loadcarname"));
        } catch (Exception e) {
            Log.d("----------->", "lineUpTo: " + e.getMessage());
        }
        return lineUp;
    }


    public static Loader loaderToBean(String json) throws Exception {
        Loader loader = new Loader();
        JSONObject temp = new JSONObject(json);
        return loaderTo(loader, temp);
    }

    public static List<Loader> loaderToBeans(String json) throws Exception {
        List<Loader> list = new ArrayList<>();
        JSONArray array = new JSONArray(json);
        Loader loader;
        for (int i = 0; i < array.length(); i++) {
            loader = new Loader();
            JSONObject temp = (JSONObject) array.get(i);
            list.add(loaderTo(loader, temp));
        }
        return list;
    }


    private static Loader loaderTo(Loader loader, JSONObject object) throws Exception {
        loader.setId(Long.valueOf(object.getString("id")));
        loader.setStatus(object.getInt("status"));
        loader.setIsis(object.getInt("isis"));
        loader.setLoadernum(object.getString("loadernum"));
        loader.setName(object.getString("name"));
        loader.setAddtime(TimeUtils.getDateTimeFromStamp(object.getLong("addtime")));
        return loader;
    }

    public static LoaderUse loaderUseToBean(String json) throws Exception {
        LoaderUse loaderUse = new LoaderUse();
        JSONObject temp = new JSONObject(json);
        return loaderUseTo(loaderUse, temp);
    }

    public static List<LoaderUse> loaderUseToBeans(String json) throws Exception {
        List<LoaderUse> list = new ArrayList<>();
        JSONArray array = new JSONArray(json);
        LoaderUse loaderUse;
        for (int i = 0; i < array.length(); i++) {
            loaderUse = new LoaderUse();
            JSONObject temp = (JSONObject) array.get(i);
            list.add(loaderUseTo(loaderUse, temp));
        }
        return list;
    }

    private static LoaderUse loaderUseTo(LoaderUse loaderUse, JSONObject object) throws Exception {
        loaderUse.setId(Long.valueOf(object.getString("id")));
        loaderUse.setIsUsesing(object.getInt("isUseing"));
        loaderUse.setUserId(object.getInt("userId"));
        loaderUse.setLoaderId(object.getInt("loaderId"));
        loaderUse.setLoaderName(object.getString("loaderName"));
        loaderUse.setUserName(object.getString("userName"));
        return loaderUse;
    }

    //Output bill

    //coal
    public static Coal coalToBean(String json) throws Exception {
        Coal coal = new Coal();
        JSONObject temp = new JSONObject(json);
        return coalTo(coal, temp);
    }

    public static List<Coal> coalToBeans(String json) throws Exception {
        List<Coal> list = new ArrayList<>();
        JSONArray array = new JSONArray(json);
        Coal coal;
        for (int i = 0; i < array.length(); i++) {
            coal = new Coal();
            JSONObject temp = (JSONObject) array.get(i);
            list.add(coalTo(coal, temp));
        }
        return list;

    }

    private static Coal coalTo(Coal coal, JSONObject object) throws Exception {
        coal.setId(Long.valueOf(object.getString("id")));
        coal.setClabbreviation(object.getString("clabbreviation"));
        coal.setTypename(object.getString("typename"));
        try {
            coal.setItemgroup(object.getString("itemgroup"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        coal.setCltype(object.getInt("cltype"));
        coal.setStatus(object.getInt("status"));
        coal.setIsis(object.getInt("isis"));
        coal.setTypeseq(object.getInt("typeseq"));
        coal.setStorage(Float.valueOf(object.getString("storage")));
        return coal;
    }

    //CarInformation
    public static CarInformation carToBean(String json) throws Exception {
        CarInformation carInfor = new CarInformation();
        JSONObject temp = new JSONObject(json);
        return carTo(carInfor, temp);
    }

    public static List<CarInformation> carToBeans(String json) throws Exception {
        List<CarInformation> list = new ArrayList<>();
        JSONArray array = new JSONArray(json);
        CarInformation carInfor;
        for (int i = 0; i < array.length(); i++) {
            carInfor = new CarInformation();
            JSONObject temp = (JSONObject) array.get(i);
            list.add(carTo(carInfor, temp));
        }
        return list;
    }

    private static CarInformation carTo(CarInformation carInfor, JSONObject object) {

        try {
            carInfor.setId(Long.valueOf(object.getString("id")));
            carInfor.setPlatenumbers(object.getString("platenumbers"));
        } catch (NumberFormatException e) {
            Log.d(Constant.COM_TAG, "carTo: car  error:信息有错");
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            carInfor.setCartype(object.getInt("cartype"));
            carInfor.setOwner(object.getString("owner"));
            carInfor.setConnectcellphone(object.getString("connectcellphone"));
            carInfor.setConnectphone(object.getString("connectphone"));
            carInfor.setConnectpeople(object.getString("connectpeople"));
            carInfor.setOwneridno(object.getString("owneridno"));
            carInfor.setOwnerlicenseno(object.getString("ownerlicenseno"));
        } catch (JSONException e) {
            Log.d(Constant.COM_TAG, "carTo: send cpu card has error:信息有错!");
            e.printStackTrace();
        }
        try {
            carInfor.setIsback(object.getInt("isback"));
            carInfor.setIsis(object.getInt("isis"));
            carInfor.setIslock(object.getInt("islock"));
            carInfor.setIssendcpucard(object.getInt("issendcpucard"));
        } catch (JSONException e) {
            Log.d(Constant.COM_TAG, "carTo: send cpu card has error:信息有错!!");
            e.printStackTrace();
        }
        try {
            carInfor.setCpucardcode(object.getString("cpucardcode"));
            carInfor.setCpucardnumber(object.getString("cpucardnumber"));
            carInfor.setSendcardtime(TimeUtils.getDateTimeFromStamp(object.getLong("sendcardtime")));
            carInfor.setSendcarduserid(object.getInt("sendcarduserid"));
            carInfor.setSendcardusername(object.getString("sendcardusername"));
            carInfor.setStatus(object.getInt("status"));
            carInfor.setAddtime(TimeUtils.getDateTimeFromStamp(object.getLong("addtime")));
        } catch (JSONException e) {
            Log.d(Constant.COM_TAG, "carTo: send cpu card has error:发卡信息有错!");
            e.printStackTrace();
        }
        return carInfor;
    }

    //test Item
    public static TestItem testItemToBean(String json) throws Exception {
        TestItem item = new TestItem();
        JSONObject temp = new JSONObject(json);
        return testItemTo(item, temp);
    }

    public static List<TestItem> testItemToBeans(String json) throws Exception {
        List<TestItem> list = new ArrayList<>();
        JSONArray array = new JSONArray(json);
        TestItem item;
        for (int i = 0; i < array.length(); i++) {
            item = new TestItem();
            JSONObject temp = (JSONObject) array.get(i);
            list.add(testItemTo(item, temp));
        }
        return list;
    }

    private static TestItem testItemTo(TestItem item, JSONObject object) throws Exception {
        item.setId(Long.valueOf(object.getString("id")));
        item.setIsis(object.getInt("isis"));
        item.setStatus(object.getInt("status"));
        item.setEnname(object.getString("enname"));
        item.setItemname(object.getString("itemname"));
        item.setItemkey(object.getString("itemkey"));
        return item;
    }

    //mobile chart
    public static MobileChart mobileChartToBean(String json) throws Exception {
        MobileChart chart = new MobileChart();
        JSONObject temp = new JSONObject(json);
        return chartTo(chart, temp);
    }

    public static List<MobileChart> mobileChartToBeans(String json) throws Exception {
        List<MobileChart> list = new ArrayList<>();
        JSONArray array = new JSONArray(json);
        MobileChart chart;
        for (int i = 0; i < array.length(); i++) {
            chart = new MobileChart();
            JSONObject temp = (JSONObject) array.get(i);
            list.add(chartTo(chart, temp));
        }
        return list;
    }

    private static MobileChart chartTo(MobileChart chart, JSONObject object) throws Exception {
        chart.setAddTime(TimeUtils.getDateTimeFromStamp(object.getLong("addTime")));
        chart.setCars(object.getInt("cars"));
        chart.setChartType(object.getInt("chartType"));
        chart.setCoals(object.getInt("coals"));
        chart.setId(Long.valueOf(object.getString("id")));
        chart.setCustomers(object.getInt("customers"));
        chart.setGrossWeight(Float.valueOf(object.getString("grossWeight")));
        chart.setLossWeight(Float.valueOf(object.getString("lossWeight")));
        chart.setNetWeight(Float.valueOf(object.getString("netWeight")));
        chart.setTraeWeight(Float.valueOf(object.getString("traeWeight")));
        chart.setStaticType(object.getInt("staticType"));
        chart.setType(object.getInt("type"));
        chart.setTimeTag(object.getString("timeTag"));
        return chart;
    }

    //Sampling
    public static Sampling samplingToBean(String json) throws Exception {
        Sampling sampling = new Sampling();
        JSONObject temp = new JSONObject(json);
        return samplingTo(sampling, temp);
    }

    public static List<Sampling> samplingToBeans(String json) throws Exception {
        List<Sampling> list = new ArrayList<>();
        JSONArray array = new JSONArray(json);
        Sampling sampling;
        for (int i = 0; i < array.length(); i++) {
            sampling = new Sampling();
            JSONObject temp = (JSONObject) array.get(i);
            list.add(samplingTo(sampling, temp));
        }
        return list;
    }

    private static Sampling samplingTo(Sampling sampling, JSONObject object) throws Exception {
        sampling.setId(object.getLong("id"));
        sampling.setSbnum(object.getString("sbnum"));
        sampling.setSbtype(object.getInt("sbtype"));
        sampling.setAddtime(TimeUtils.getDateTimeFromStamp(object.getLong("addtime")));
        sampling.setAdduserid(object.getInt("adduserid"));
        sampling.setAddusername(object.getString("addusername"));
        sampling.setCarnumbers(object.getString("carnumbers"));
        sampling.setCoalfactory(object.getString("coalfactory"));
        sampling.setCoalfactoryid(object.getInt("coalfactoryid"));
        sampling.setCoalsettleprice(Float.valueOf(object.getString("coalsettleprice")));
        try {
            sampling.setSamplinguname(object.getString("samplinguname"));
            sampling.setCoalspec(object.getString("coalspec"));
            sampling.setSamplingtime(TimeUtils.getDateTimeFromStamp(object.getLong("samplingtime")));
        } catch (Exception e) {
            Log.d("---------", "samplingTo: Error " + e.getMessage());
        }
        sampling.setCoaltype(object.getString("coaltype"));
        sampling.setCoaltypeid(object.getInt("coaltypeid"));
        sampling.setCoalunitprice(Float.valueOf(object.getString("coalunitprice")));
        sampling.setContractid(object.getInt("contractid"));
        sampling.setCtnum(object.getString("ctnum"));
        sampling.setIscoalsettle(object.getInt("iscoalsettle"));
        sampling.setIsis(object.getInt("isis"));
        sampling.setStatus(object.getInt("status"));
        sampling.setIstest(object.getInt("istest"));
        sampling.setRemark(object.getString("remark"));
        sampling.setSettleresoultid(object.getInt("settleresoultid"));
        sampling.setSupplycpname(object.getString("supplycpname"));
        sampling.setSupplycpid(object.getInt("supplycpid"));
        sampling.setWeighbillids(object.getString("weighbillids"));
        sampling.setWeighbillnums(object.getString("weighbillnums"));
        sampling.setWeights(Float.valueOf(object.getString("weights")));
        sampling.setWeightscarnum(object.getInt("weightscarnum"));
        return sampling;
    }

    //fuel diary
    public static Fueldiary fueldiaryToBean(String json) throws Exception {
        Fueldiary fueldiary = new Fueldiary();
        JSONObject temp = new JSONObject(json);
        return fueldiaryTo(fueldiary, temp);
    }

    public static List<Fueldiary> fueldiaryToBeans(String json) throws Exception {
        List<Fueldiary> list = new ArrayList<>();
        JSONArray array = new JSONArray(json);
        Fueldiary fueldiary;
        for (int i = 0; i < array.length(); i++) {
            fueldiary = new Fueldiary();
            JSONObject temp = (JSONObject) array.get(i);
            list.add(fueldiaryTo(fueldiary, temp));
        }
        return list;
    }

    private static Fueldiary fueldiaryTo(Fueldiary fueldiary, JSONObject object) throws Exception {
        fueldiary.setId(object.getLong("id"));
        fueldiary.setNumber(object.getString("number"));
        fueldiary.setAddtime(TimeUtils.getDateTimeFromStamp(object.getLong("addtime")));
        fueldiary.setAdduserid(object.getInt("adduserid"));
        fueldiary.setAddusername(object.getString("addusername"));
        fueldiary.setCarid(object.getInt("carid"));
        fueldiary.setIscalc(object.getInt("iscalc"));
        fueldiary.setIsis(object.getInt("isis"));
        fueldiary.setIsoauth(object.getInt("isoauth"));
        fueldiary.setType(object.getInt("type"));
        fueldiary.setStatus((byte) object.getInt("status"));
        fueldiary.setPlatenumbers(object.getString("platenumbers"));
        fueldiary.setOilmass(Float.valueOf(object.getString("oilmass")));
        fueldiary.setTotalmoney(Float.valueOf(object.getString("totalmoney")));
        fueldiary.setUnitprice(Float.valueOf(object.getString("unitprice")));
        try {
            fueldiary.setOauthtime(TimeUtils.getDateTimeFromStamp(object.getLong("oauthtime")));
            fueldiary.setOauthuserid(object.getInt("oauthuserid"));
            fueldiary.setOauthusername(object.getString("oauthusername"));
        } catch (Exception e) {
            Log.d("-------->", "fuel diary To: option data error " + e.getMessage());
        }
        return fueldiary;
    }

    //storage
    public static Storage storageToBean(String dataJson) throws Exception {
        Storage storage = new Storage();
        JSONObject obj = new JSONObject(dataJson);
        storage.setId(Long.valueOf(obj.getString("id")));
        storage.setLastintime(TimeUtils.getDateTimeFromStamp(obj.getLong("lastintime")));
        storage.setVolume(Float.valueOf(obj.getString("volume")));
        storage.setTonnage(Float.valueOf(obj.getString("tonnage")));
        return storage;
    }

    //oil price
    public static OilPrice oilPriceToBean(String json) throws Exception {
        OilPrice oilPrice = new OilPrice();
        JSONObject temp = new JSONObject(json);
        return oilPriceTo(oilPrice, temp);
    }

    public static List<OilPrice> oilPriceToBeans(String json) throws Exception {
        List<OilPrice> list = new ArrayList<>();
        JSONArray array = new JSONArray(json);
        OilPrice oilPrice;
        for (int i = 0; i < array.length(); i++) {
            oilPrice = new OilPrice();
            JSONObject temp = (JSONObject) array.get(i);
            list.add(oilPriceTo(oilPrice, temp));
        }
        return list;
    }

    private static OilPrice oilPriceTo(OilPrice oilPrice, JSONObject object) throws Exception {
        oilPrice.setId(object.getLong("id"));
        oilPrice.setAdduserid(object.getInt("adduserid"));
        oilPrice.setAddusername(object.getString("addusername"));
        oilPrice.setIsis(object.getInt("isis"));
        oilPrice.setStatus(object.getInt("status"));
        oilPrice.setOiltype(object.getString("oiltype"));
        oilPrice.setAddtime(TimeUtils.getDateTimeFromStamp(object.getLong("addtime")));
        oilPrice.setStarttime(TimeUtils.getDateTimeFromStamp(object.getLong("starttime")));
        oilPrice.setUnitprice(Float.valueOf(object.getString("unitprice")));
        try {
            oilPrice.setEndtime(TimeUtils.getDateTimeFromStamp(object.getLong("endtime")));
        } catch (Exception e) {
            Log.d("-------->", "oil price  To: option data error " + e.getMessage());
        }
        return oilPrice;
    }

    public static CarFinance carFinanceToBean(String dataJson) throws Exception {
        CarFinance finance = JSON.parseObject(dataJson, CarFinance.class);
        return finance;
    }
}
